using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Integrator.DAL.MESSAGE;
using System.Net.Sockets;
using System.Text;

/// <summary>
/// Summary description for BusinessProcess
/// </summary>
public class BusinessProcess
{
    public BusinessProcess()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public BaseResponse Process(BaseRequest request)
    {
        BaseResponse response = null;
        string reqContent = request.ToString();

        string resContent = SendMessage("127.0.0.1", 8080, reqContent);

        if (request.GetType().Equals(typeof(BalanceEnquiryRequest)))
        {
            response = new BalanceEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(BillInfoEnquiryRequest)))
        {
            response = new BillInfoEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(BillListOfCifEnquiryRequest)))
        {
            response = new BillListOfCifEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(BranchEnquiryRequest)))
        {
            response = new BranchEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(CasaTransHistoryEnquiryRequest)))
        {
            response = new CasaTransHistoryEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(CashWithdrawalRequest)))
        {
            response = new CashWithdrawalResponse();
        }
        else if (request.GetType().Equals(typeof(CustomerInquiryRequest)))
        {
            response = new CustomerInquiryResponse();
        }
        else if (request.GetType().Equals(typeof(CustomerRelationInquiryRequest)))
        {
            response = new CustomerRelationInquiryResponse();
        }
        else if (request.GetType().Equals(typeof(EarmarkReleaseRequest)))
        {
            response = new EarmarkReleaseResponse();
        }
        else if (request.GetType().Equals(typeof(EarmarkReleaseTransRequest)))
        {
            response = new EarmarkReleaseTransResponse();
        }
        else if (request.GetType().Equals(typeof(EarmarkRequest)))
        {
            response = new EarmarkResponse();
        }
        else if (request.GetType().Equals(typeof(EarnMartTransRevertRequest)))
        {
            response = new EarnMartTransRevertResponse();
        }
        else if (request.GetType().Equals(typeof(ExchangeRateEnquiryRequest)))
        {
            response = new ExchangeRateEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(FinancialPostingRequest)))
        {
            response = new FinancialPostingResponse();
        }
        else if (request.GetType().Equals(typeof(FinancialPostingReversalRequest)))
        {
            response = new FinancialPostingReversalResponse();
        }
        else if (request.GetType().Equals(typeof(FinancialPostingRevertRequest)))
        {
            response = new FinancialPostingRevertResponse();
        }
        else if (request.GetType().Equals(typeof(FundTransferInterBankRequest)))
        {
            response = new FundTransferInterBankResponse();
        }
        else if (request.GetType().Equals(typeof(FundTransferRequest)))
        {
            response = new FundTransferResponse();
        }
        else if (request.GetType().Equals(typeof(FxrateEnquiryRequest)))
        {
            response = new FxrateEnquiryResponse();
        }

        else if (request.GetType().Equals(typeof(LcInfoEnquiryRequest)))
        {
            response = new LcInfoEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(LcListOfCifEnquiryRequest)))
        {
            response = new LcListOfCifEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(LoanDisbursementScheduleRequest)))
        {
            response = new LoanDisbursementScheduleResponse();
        }
        else if (request.GetType().Equals(typeof(LoanEnquiryRequest)))
        {
            response = new LoanEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(LoanQueryRequest)))
        {
            response = new LoanQueryResponse();
        }
        else if (request.GetType().Equals(typeof(LoanRepaymentScheduleRequest)))
        {
            response = new LoanRepaymentScheduleResponse();
        }
        else if (request.GetType().Equals(typeof(LoanStatementRequest)))
        {
            response = new LoanStatementResponse();
        }
        else if (request.GetType().Equals(typeof(MarchantPortalRequest)))
        {
            response = new MarchantPortalResponse();
        }
        else if (request.GetType().Equals(typeof(ReversalCashWithdrawalRequest)))
        {
            response = new ReversalCashWithdrawalResponse();
        }
        else if (request.GetType().Equals(typeof(ReversalFundTranferRequest)))
        {
            response = new ReversalFundTranferResponse();
        }
        else if (request.GetType().Equals(typeof(ReversalMerchantPortalRequest)))
        {
            response = new ReversalMerchantPortalResponse();
        }
        else if (request.GetType().Equals(typeof(SIBookingOneTimeRequest)))
        {
            response = new SIBookingOneTimeResponse();
        }
        else if (request.GetType().Equals(typeof(SIBookingRecurringRequest)))
        {
            response = new SIBookingRecurringResponse();
        }
        else if (request.GetType().Equals(typeof(SIBookingRequest)))
        {
            response = new SIBookingResponse();
        }
        else if (request.GetType().Equals(typeof(SICancellationRequest)))
        {
            response = new SICancellationResponse();
        }
        else if (request.GetType().Equals(typeof(SWBookingRequest)))
        {
            response = new SWBookingResponse();
        }
        else if (request.GetType().Equals(typeof(SoCancellationRequest)))
        {
            response = new SoCancellationResponse();
        }
        else if (request.GetType().Equals(typeof(SoDetailRequest)))
        {
            response = new SoDetailResponse();
        }
        else if (request.GetType().Equals(typeof(StatementEnquiryRequest)))
        {
            response = new StatementEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(TideInterestRateEnquiryRequest)))
        {
            response = new TideInterestRateEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(TideDespositEnquiryRequest)))
        {
            response = new TideDespositEnquiryResponse();
        }
        else if (request.GetType().Equals(typeof(TxnRevertRequest)))
        {
            response = new TxnRevertResponse();
        }        
        response.FromString(resContent);
        return new BaseResponse();
    }

    /// <summary>
    /// Ham truy cap Core Banking qua giao thuc TCP/IP
    /// </summary>
    /// <param name="ip"></param>
    /// <param name="port"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public string SendMessage(string ip, int port, string message)
    {
        TcpClient tcpClient = new TcpClient();
        NetworkStream networkStream;
        try
        {            
            System.Net.IPEndPoint myEP = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ip), port);
            tcpClient.SendTimeout = 60;
            tcpClient.NoDelay = true;
            tcpClient.Connect(myEP);
            networkStream = tcpClient.GetStream();

            if (networkStream.CanWrite & networkStream.CanRead)
            {
                message = message.Remove(message.Length - 2, 2);
                Byte[] sendBytes = Encoding.UTF8.GetBytes(message);
                networkStream.Write(sendBytes, 0, sendBytes.Length);
                byte[] bytes;
                string returndata = "";
                tcpClient.ReceiveBufferSize = 8912 * 5;
                bytes = new byte[tcpClient.ReceiveBufferSize + 1];
                networkStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
                returndata = Encoding.UTF8.GetString(bytes);
                tcpClient.Close();
                tcpClient = null;
                networkStream.Close();
                networkStream = null;
                return returndata.Trim().Replace("\0", "");
            }
            else
            {
                tcpClient.Close();
                tcpClient = null;
                networkStream.Close();
                networkStream = null;
                return "";
            }
        }
        catch (Exception ex)
        {
            tcpClient = null;
            networkStream = null;
            throw ex;
        }
    }
}
